package pers.cj.pdos.controller.analysis;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.entity.BusinessSet;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.mapper.SaleAnalysisMapper;
import pers.cj.pdos.service.ISaleAnalysisService;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.UserVo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Controller
@RequestMapping("/financeCount")
public class OperateAnalysisController extends BaseController {
    @Autowired
    ISaleAnalysisService saleAnalysisService;

    @RequiresPermissions("operateAnalysis:query")
    @RequestMapping("/operateAnalysis")
    public String profitAnalysis() {
        return "analysis/operate/operateAnalysis";
    }

    @ResponseBody
    @RequestMapping("/operateAnalysis/getTrend")
    public ResponseData getTrend(String storeId,String flag,String day,String month,String year) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();

        String date1=null;
        String date2=null;

        BigDecimal days = null;

        Map<String, Object> chatMap = new LinkedHashMap<>();

        if ("day".equals(flag)){
            days = new BigDecimal(7);
            date2 = day;
            Date date = DateUtils.genDate(day, DateUtils.YYYY_MM_DD);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -6);
            date1 = DateUtils.getDate(calendar.getTime(), DateUtils.YYYY_MM_DD);

            for (int i=0;i<7;i++) {
                chatMap.put(calendar.get(Calendar.DAY_OF_MONTH)+"日", 0);
                calendar.add(Calendar.DATE, 1);
            }
        } else if ("month".equals(flag)) {
            Date date = DateUtils.genDate(month, DateUtils.YYYY_MM);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            days = new BigDecimal(maxDay);
            date1 = month+"-01";
            date2 = month+"-"+maxDay;

            for (int i=1;i<=maxDay;i++) {
                chatMap.put(i+"日", 0);
            }
        } else if ("year".equals(flag)) {
            Date date = DateUtils.genDate(year, DateUtils.YYYY);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            days = new BigDecimal(calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
            date1 = year+"-01-01";
            date2 = year+"-12-31";

            for (int i=1; i <= 12; i++) {
                chatMap.put(i+"月", 0);
            }
        }


        if ("year".equals(flag)) {
            //按照月获取总销售额
            List<Map> mapList = saleAnalysisService.getTotalByMonth(company.getId(),null, null, null,PdosUtils.getViewOtherStoreId(userVo,storeId),null, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("month")+"月", map);
            }
        } else {
            //按照日获取总销售额
            List<Map> mapList = saleAnalysisService.getTotalByDate(company.getId(),null, null, null,PdosUtils.getViewOtherStoreId(userVo,storeId),null, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("date")+"日", map);
            }
        }




        //labels xData  series
        List<String> labels = new ArrayList<>();
        labels.add("销售额");
        labels.add("毛利");

        List<String> xData = new ArrayList<>();
        List<BigDecimal> saleList = new ArrayList<>();
        List<BigDecimal> profitsList = new ArrayList<>();

        BigDecimal zero = new BigDecimal(0);
        for (Map.Entry<String, Object> entry : chatMap.entrySet()) {
            xData.add(entry.getKey());

            if (entry.getValue() instanceof Map) {
                saleList.add((BigDecimal) ((Map) entry.getValue()).get("amount"));
                profitsList.add((BigDecimal) ((Map) entry.getValue()).get("profits"));
            } else {
                saleList.add(zero);
                profitsList.add(zero);
            }
        }
        List<Map> series = new ArrayList<>();
        Map<String, Object> saleMap = new HashMap<>();
        saleMap.put("name","销售额");
        saleMap.put("type","line");
        saleMap.put("data",saleList);
        series.add(saleMap);

        Map<String, Object> profitsMap = new HashMap<>();
        profitsMap.put("name","毛利");
        profitsMap.put("type","line");
        profitsMap.put("data",profitsList);
        series.add(profitsMap);

        Map<String, Object> map = new HashMap<>();
        map.put("labels",labels);
        map.put("xData",xData);
        map.put("series",series);


        Map total_map = saleAnalysisService.getTotalData(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), null, Utils.genDate(date1), Utils.genDate(date2));
        BigDecimal trend_number = new BigDecimal(total_map.get("total_sale_number")+"");
        BigDecimal trend_amount = (BigDecimal) total_map.get("total_sale_amt");
        BigDecimal trend_profits = (BigDecimal) total_map.get("total_sale_profits");

        map.put("avgNumber",trend_number.divide(days, 4, RoundingMode.HALF_UP));
        map.put("avgAmount",trend_amount.divide(days, 4,  RoundingMode.HALF_UP));
        map.put("avgProfits",trend_profits.divide(days, 4,  RoundingMode.HALF_UP));

        if ("day".equals(flag)){
            total_map = saleAnalysisService.getTotalData(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), null, Utils.genDate(day), Utils.genDate(day));
            map.put("number", total_map.get("total_sale_number"));
            map.put("amount", total_map.get("total_sale_amt"));
            map.put("profits", total_map.get("total_sale_profits"));
        } else {
            map.put("number", trend_number);
            map.put("amount", trend_amount);
            map.put("profits", trend_profits);
        }

        return ResponseData.success(map);
    }
}
